<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserLevel;

class Up
{
    protected $order;
    protected $set;

    public function __construct(Order $order) {
        $this->order = $order;
        $this->set = SetUserUp::getLastOne();

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 只有报单区才可以升级
        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT'])) {
            // 后台设置的参数
            // $this->set = SetUserUp::getLastOne();


            // 升级
            $this->up();



        }

        // 累计金额升级
        // 复购区也算
        $this->upTotal();
    }

    protected function up()
    {
        $total_price = $this->order->total_pay - $this->order->total_freight;

        if ($total_price == $this->set->vip4) {
            $user = User::findOne(['id' => $this->order->user_id]);
            if ($user && $user->level < UserLevel::$keys['vip4']) {
                $user->level = UserLevel::$keys['vip4'];
                $user->save();
            }
        } else if ($total_price == $this->set->vip3) {
            $user = User::findOne(['id' => $this->order->user_id]);
            if ($user && $user->level < UserLevel::$keys['vip3']) {
                $user->level = UserLevel::$keys['vip3'];
                $user->save();
            }
        } else if ($total_price == $this->set->vip2) {
            $user = User::findOne(['id' => $this->order->user_id]);
            if ($user && $user->level < UserLevel::$keys['vip2']) {
                $user->level = UserLevel::$keys['vip2'];
                $user->save();
            }
        } else if ($total_price == $this->set->vip1) {
            $user = User::findOne(['id' => $this->order->user_id]);
            if ($user && $user->level < UserLevel::$keys['vip1']) {
                $user->level = UserLevel::$keys['vip1'];
                $user->save();
            }
        }
    }

    public function upTotal()
    {
        $user = User::findOne(['id' => $this->order->user_id]);
        $total = $this->_getAmount($user->id);

        // var_dump($total, $this->set->vip4, $total>=$this->set->vip4);exit;

        // var_dump($user->id, $user->level);exit;



        if ($user->level < UserLevel::$keys['vip4']) {
            if ($total >= $this->set->vip4) {
                $user->level = UserLevel::$keys['vip4'];
                $user->save();
            }
        }

        if ($user->level < UserLevel::$keys['vip3']) {
            if ($total >= $this->set->vip3) {
                $user->level = UserLevel::$keys['vip3'];
                $user->save();
            }
        }

        if ($user->level < UserLevel::$keys['vip2']) {
            if ($total >= $this->set->vip2) {
                $user->level = UserLevel::$keys['vip2'];
                $user->save();
            }
        }

        if ($user->level < UserLevel::$keys['vip1']) {
            if ($total >= $this->set->vip1) {
                $user->level = UserLevel::$keys['vip1'];
                $user->save();
            }
        }
    }

    private function _getAmount($user_id)
    {
        // 本含此次订单
        $total = Order::find()
            ->where(['user_id' => $user_id])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->andWhere(['!=', 'status', Order::$statusMapping['REFUND']])
            ->sum('total_pay');

        return (float)$total;
    }

}